<?php
// This file is used to insert a new decorations via AJAX calls
// TODO - once Elgg provides an easy way to load session information w/o the entire lib,
//          remove the large includes.php file, as none of the rest depends on libs outside
//          of mysql support & generic php.


// Headers are needed to force IE to not cache results.
header('Cache-Control: no-cache, no-store, must-revalidate'); //HTTP/1.1
header('Expires: Sun, 01 Jul 2005 00:00:00 GMT');
header('Pragma: no-cache'); //HTTP/1.0

require_once('../../../includes.php');

require_once('../view/page.php');
require_once('../view/PageEditor.php');
require_once('../model/decoration.php');


// Current user
$userid = $_SESSION['userid'];

// Decoration to create
$d = new folio_decoration;

$d->page_ident = intval( $_POST['page_ident'] );
$d->type = $_POST['type'];

if ( isset( $_POST['text'] ) ) {
    $d->text = $_POST['text'];
} else {
    $d->text = '';
}

    // Validate permissions
    $page = folio_page::SelectWherePageIdent( $d->page_ident );

    if ( !$page->Permission('update') ) {
        trigger_error('You do not have permission to edit this page', E_USER_ERROR);
    }

// Insert into DB
$d = folio_decoration::Insert( $d );
//var_dump( $d );
$decorations = array();
$decorations[] = $d;

// Insert into Editor
$html = folio_PageEditor::Add( $decorations ) ;

echo $html;

?>
